#include "shopper.h"
#include "logistics.h"
#include "account.h"
#include "vip.h"
#include <stdlib.h>
#include <string.h>

static int s_vip_index;

double sum_money(void)
{
	double money = 0;
	puts("结束请输入：0000");
	for(;;)
	{
		printf("请输入商品ID编号：");
		char id[10];
		get_str(id,sizeof(id));
		if(0 == strcmp(id,"0000"))
		{
			printf("金额总共：%.2lf元\n",money);
			return money;
		}
		int index = query_goods_id(id);
		if(-1 == index)
		{
			printf("无该编号商品！请检查！\n");
			continue;
		}
		if(0 == goods[index].nums)
		{
			printf("该商品已售完！请检查！\n");
			continue;
		}
		money += goods[index].price;
		goods[index].nums--;
		printf("购物成功！商品编号：%-15s名称：%-21s单价：%.2lf元\n",
			goods[index].id,
			goods[index].name,
			goods[index].price);
	}
}

void add_records(double money)
{
	for(;;)
	{
		printf("请输入付款账户手机号：");
		get_str(records[g_records_cnt].payment,sizeof(records[g_records_cnt].payment));
		if(11 != strspn(records[g_records_cnt].payment,STR_DIGIT))
		{
			printf("手机号格式有误！请重新输入！\n");
			continue;
		}
		break;
	}
	printf("支付成功！\n");
	// 检查是否为VIP
	int index = query_phone(records[g_records_cnt].payment);
	if(-1 < index)
	{
		vip[index].points += (unsigned int)money;
		printf("本次购物获得%u积分,目前账户积分为：%u\n",(unsigned int)money,vip[index].points);
	}
	strcpy(records[g_records_cnt].collection,records[0].collection);
	records[g_records_cnt].money = money;
	g_records_cnt++;
	print_sec("欢迎下次购物！(五秒后跳转)\n",5);
	return;
}

void settlement(void)
{
	double money = sum_money();
	if(0 == money)
	{
		print_sec("你是故意找茬是不是。:)",2);
		return;
	}
	add_records(money);
	return;
}

bool login_vip(void)
{
	printf("请输入手机号：");
	char phone[13];
	get_str(phone,sizeof(phone));
	s_vip_index = query_phone(phone);
	if(-1 == s_vip_index)
	{
		print_sec("该手机号非会员！",0.75);
		return false;
	}

	printf("请输入密码：");
	char password[12];
	get_str(password,sizeof(password));
	if(0 != strcmp(password,vip[s_vip_index].password))
	{
		print_sec("密码错误！",0.75);
		return false;
	}
	print_sec("登录成功！",0.75);
	return true;
}

void menu_vip_point(void)
{
	if(!login_vip()) return;
	for(;;)
	{
		system("clear");
		puts("*****会员中心*****");
		puts("1、积分查看");
		puts("2、礼品兑换");
		puts("3、返回");
		switch(get_cmd('1','3'))
		{
			case '1': show_vip_point(); break;
			case '2': menu_gifts(); break;
			case '3':
					return;
		}
	}

}

void show_vip_point(void)
{
	printf("您的积分为：%u\n",vip[s_vip_index].points);
	anykey_continue();
	return;
}

void menu_gifts(void)
{
	for(;;)
	{
		system("clear");
		puts("*****礼品兑换*****");
		puts("1、大师兄限定教材视频——积分20000");
		puts("2、50元优惠券——积分300");
		puts("3、返回");
		switch(get_cmd('1','3'))
		{
			case '1': 
				{	
					if(20000 > vip[s_vip_index].points)
					{
						print_sec("您的积分不足！无法兑换！",0.75);
				   		break;
					}
					vip[s_vip_index].points -= 20000;
					print_sec("兑换成功！",0.75);
					break;
				}
			case '2':
				{	
					if(300 > vip[s_vip_index].points)
					{
						print_sec("您的积分不足！无法兑换！",0.75);
				   		break;
					}
					vip[s_vip_index].points -= 300;
					print_sec("兑换成功！",0.75);
					break;
				}
			case '3':
					return;
		}
	}
}


void menu_shopper(void)
{
	for(;;)
	{
		system("clear");
		puts("*****销售信息管理*****");
		puts("1、付款");
		puts("2、会员中心");
		puts("3、返回");
		switch(get_cmd('1','3'))
		{
			case '1': settlement(); break;
			case '2': menu_vip_point(); break;
			case '3':
					return;
		}
	}
}
